package com.miao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miao.base.exception.XueChengPlusException;
import com.miao.dto.BindMediaAndTeachPlanDto;
import com.miao.dto.TeachPlanDto;
import com.miao.mapper.TeachplanMapper;
import com.miao.mapper.TeachplanMediaMapper;
import com.miao.pojo.Teachplan;
import com.miao.pojo.TeachplanMedia;
import com.miao.service.ITeachplanService;
import jdk.nashorn.internal.ir.CallNode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements ITeachplanService {

    @Autowired
    private TeachplanMapper teachplanMapper;

    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;

    //根据id查询课程计划，返回树形结构！
    public List<TeachPlanDto> selectTeachById(Long courseId) {
        return teachplanMapper.selectPlanById(courseId);
    }

    //新增大小课程以及修改课程的
    @Transactional
    @Override
    public void savePlan(TeachPlanDto teachPlanDto) {
        //根据id是否为空，判断是添加还是新增
        Long id = teachPlanDto.getId();
        if(id == null){
            //新增
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(teachPlanDto,teachplan);
            //确定teachplan的排序字段：找到同级节点个数+1
            Long parentid = teachplan.getParentid();
            Long courseId = teachplan.getCourseId();
            LambdaQueryWrapper<Teachplan> teachplanLambdaQueryWrapper = new LambdaQueryWrapper<>();
            teachplanLambdaQueryWrapper.eq(Teachplan::getParentid,parentid);
            teachplanLambdaQueryWrapper.eq(Teachplan::getCourseId,courseId);
            teachplanLambdaQueryWrapper.orderByDesc(Teachplan::getOrderby);
            List<Teachplan> teachplans = teachplanMapper.selectList(teachplanLambdaQueryWrapper);
            if(teachplans.size() == 0){
                teachplan.setOrderby(1);
            }else{
                teachplan.setOrderby(teachplans.get(0).getOrderby() + 1);
            }
            teachplanMapper.insert(teachplan);
        }else{
            //修改
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachPlanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }
    }

    //根据id删除课程计划
    @Transactional
    @Override
    public void deletePlan(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        Long parentid = teachplan.getParentid();
        if(parentid == 0){
            //大章节
            //判断是否有子章节
            LambdaQueryWrapper<Teachplan> teachplanLambdaQueryWrapper = new LambdaQueryWrapper<>();
            teachplanLambdaQueryWrapper.eq(Teachplan::getParentid,id);
            teachplanLambdaQueryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId());
            Integer count = teachplanMapper.selectCount(teachplanLambdaQueryWrapper);
            if(count == 0){
                //没有子章节
                teachplanMapper.deleteById(id);
            }else{
                //有子章节
                throw new XueChengPlusException("该章节还有子章节！不可删除！");
            }
        }else{
            //小章节
            //删除关联的视频
            LambdaQueryWrapper<TeachplanMedia> teachplanMediaLambdaQueryWrapper = new LambdaQueryWrapper<>();
            teachplanMediaLambdaQueryWrapper.eq(TeachplanMedia::getTeachplanId,id);
            teachplanMediaMapper.delete(teachplanMediaLambdaQueryWrapper);
            //删除小节本身
            teachplanMapper.deleteById(id);
        }

    }

    //将课程计划上移
    @Transactional
    @Override
    public void moveUp(Long id) {
        //判断是否再最上面
        Teachplan teachplan = teachplanMapper.selectById(id);
        Integer orderby = teachplan.getOrderby();
        Long parentid = teachplan.getParentid();
        Long courseId = teachplan.getCourseId();
        LambdaQueryWrapper<Teachplan> lambadQuery = new LambdaQueryWrapper<>();
        lambadQuery.eq(Teachplan::getParentid,parentid);
        lambadQuery.eq(Teachplan::getCourseId,courseId);
        lambadQuery.lt(Teachplan::getOrderby,orderby);
        lambadQuery.orderByDesc(Teachplan::getOrderby);
        List<Teachplan> teachplans = teachplanMapper.selectList(lambadQuery);
        if(teachplans.size() == 0){
            throw new XueChengPlusException("该章节已经最上了！！！");
        }else{
            //不在最顶上
            Teachplan _teachPlan = teachplans.get(0);
            Integer temp = _teachPlan.getOrderby();
            _teachPlan.setOrderby(orderby);
            teachplanMapper.updateById(_teachPlan);
            teachplan.setOrderby(temp);
            teachplanMapper.updateById(teachplan);

        }
    }
    //课程计划下移动
    @Transactional
    @Override
    public void moveDown(Long id) {
        //判断是否再最下面
        Teachplan teachplan = teachplanMapper.selectById(id);
        Integer orderby = teachplan.getOrderby();
        Long parentid = teachplan.getParentid();
        Long courseId = teachplan.getCourseId();
        //查询总共几个课程
        LambdaQueryWrapper<Teachplan> lambadQuery = new LambdaQueryWrapper<>();
        lambadQuery.eq(Teachplan::getParentid,parentid);
        lambadQuery.eq(Teachplan::getCourseId,courseId);
        lambadQuery.gt(Teachplan::getOrderby,orderby);
        lambadQuery.orderByAsc(Teachplan::getOrderby);
        List<Teachplan> teachplans = teachplanMapper.selectList(lambadQuery);
        if(teachplans.size() == 0){
            //已经再最下面
            throw new XueChengPlusException("已经在最下面了！！不能下移动了！");
        }else{
            //不在最顶上
            Teachplan _teachPlan = teachplans.get(0);
            Integer temp = _teachPlan.getOrderby();
            _teachPlan.setOrderby(orderby);
            teachplanMapper.updateById(_teachPlan);
            teachplan.setOrderby(temp);
            teachplanMapper.updateById(teachplan);

        }
    }
    //绑定媒资信息和课程信息
    @Transactional
    @Override
    public void associateWithMedia(BindMediaAndTeachPlanDto bindMediaAndTeachPlanDto) {
        //课程id
        Long teachplanId = bindMediaAndTeachPlanDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        Long courseId = teachplan.getId();
        if(courseId == null){
            XueChengPlusException.cast("课成计划不存在！");
            return;
        }
        //删除原有的课程信息
        LambdaQueryWrapper<TeachplanMedia> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeachplanMedia::getTeachplanId,bindMediaAndTeachPlanDto.getTeachplanId());
        teachplanMediaMapper.delete(wrapper);
        //添加新纪录
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindMediaAndTeachPlanDto, teachplanMedia);
        teachplanMedia.setMediaFilename(bindMediaAndTeachPlanDto.getFileName());
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);

    }
}
